﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using ProgNetComponents.Classes;

namespace ProgNetComponents.Validation.Rules
{
    /// <summary>
    /// Unique and required validation rule
    /// </summary>
    public class ColumnUniqueRequiredValidationRule : MultiValidationRule
    {
        /// <summary>
        /// Gets the column required rule.
        /// </summary>
        /// <value>
        /// The column required rule.
        /// </value>
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public ColumnRequiredValidationRule ColumnRequiredRule
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets the column unique rule.
        /// </summary>
        /// <value>
        /// The column unique rule.
        /// </value>
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public ColumnUniqueValidationRule ColumnUniqueRule
        {
            get;
            private set;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ColumnUniqueRequiredValidationRule"/> class.
        /// </summary>
        public ColumnUniqueRequiredValidationRule()
        {
            ColumnRequiredRule = new ColumnRequiredValidationRule();
            ColumnUniqueRule = new ColumnUniqueValidationRule();
            AddRule(ColumnRequiredRule);
            AddRule(ColumnUniqueRule);
        }

        /// <summary>
        /// Gets or sets the <see cref="T:System.ComponentModel.ISite" /> of the <see cref="T:System.ComponentModel.Component" />.
        /// </summary>
        /// <returns>The <see cref="T:System.ComponentModel.ISite" /> associated with the <see cref="T:System.ComponentModel.Component" />, or null if the <see cref="T:System.ComponentModel.Component" /> is not encapsulated in an <see cref="T:System.ComponentModel.IContainer" />, the <see cref="T:System.ComponentModel.Component" /> does not have an <see cref="T:System.ComponentModel.ISite" /> associated with it, or the <see cref="T:System.ComponentModel.Component" /> is removed from its <see cref="T:System.ComponentModel.IContainer" />.</returns>
        public override ISite Site
        {
            get
            {
                return base.Site;
            }
            set
            {
                base.Site = value;
                if (value != null)
                {
                    if (ColumnRequiredRule.Site == null)
                        ColumnRequiredRule.Site = new FakeSite(ColumnRequiredRule, value);
                    if (ColumnUniqueRule.Site == null)
                        ColumnUniqueRule.Site = new FakeSite(ColumnUniqueRule, value);

                    if (ColumnRequiredRule.Site != null)
                        ColumnRequiredRule.Site.Name = value.Name + ".ColumnRequiredRule";
                    if (ColumnUniqueRule.Site != null)
                        ColumnUniqueRule.Site.Name = value.Name + ".ColumnUniqueRule";
                }
            }
        }
    }
}
